package com.example.administrator.demorecyclerview.refresh;

import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Created by Administrator on 2018/3/8.
 */

public class RefreshHeaderView extends LinearLayout implements RefreshHeader {
    public RefreshHeaderView(Context context) {
        super(context);
        init();
    }

    public RefreshHeaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RefreshHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private TextView mTextView;

    private void init() {
        setOrientation(HORIZONTAL);
        LayoutParams paramsText = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mTextView = new AppCompatTextView(getContext());
        mTextView.setTextSize(40f);
        addView(mTextView, paramsText);
        LayoutParams paramsProgress = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ProgressBar progressBar = new ProgressBar(getContext());
        addView(progressBar, paramsProgress);
        setGravity(Gravity.CENTER);
    }

    @Override
    public void onThroughHeader(int distance, boolean isRefreshing, boolean backScroll) {
//        Log.i("@@@", "@@@onArriveHeader:" + distance + ",isRefreshing:" + isRefreshing + ",isBack:" + backScroll);

        if (!backScroll) {
            if (!isRefreshing) {
                if (!refreshingStarted(distance)) {
                    mTextView.setText("下拉后刷新");
                } else {
                    mTextView.setText("释放后刷新");

                }
            }

        }
//        float scale = (float) -distance / getMeasuredHeight();
//        mTextView.setRotation(360*scale);
//        Log.i("@@@", "@@@@@@@scale:" + scale);
//        mTextView.setScaleX(scale);
//        mTextView.setScaleY(scale);
//        mTextView.setTextScaleX(scale);
    }


    @Override
    public boolean onStartRefreshing(int distance) {

        return refreshingStarted(distance);
    }

    @Override
    public void onRefreshingAccepted() {
        mTextView.setText("正在刷新...");
    }


    private boolean refreshingStarted(int distance) {
        if (distance > -getMeasuredHeight()) {
            return false;
        }
        return true;
    }


    @Override
    public void onFinishRefreshing() {
        mTextView.setText("刷新完成");
    }


}
